package com.stupro.cjlu.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stupro.cjlu.common.vo.*;
import com.stupro.cjlu.config.themis.annotation.Student;
import com.stupro.cjlu.mapper.StudentcourseMapper;
import com.stupro.cjlu.entity.Studentcourse;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class StudentCourseDAO {
    public static final int PAGE_SIZE = 10;
    private final StudentcourseMapper mapper;

    public StudentCourseDAO(StudentcourseMapper mapper) {
        this.mapper = mapper;
    }

    public int insert(Studentcourse entity) {

        return mapper.insert(entity);
    }

    public int delete(Integer id) {
        return mapper.deleteById(id);
    }

    public Studentcourse get(Integer id) {
        return mapper.selectById(id);
    }

    public int update(Studentcourse entity) {
        return mapper.updateById(entity);
    }

    public int count(String className, String courseName, String studentName) {
        return mapper.count(className, courseName, studentName);
    }


    public IPage<StudentCourseItemVO> getCourse(Integer index, String className, String courseName, String studentName,
                                                int teacherID) {
        Page<StudentCourseItemVO> page = new Page<>(index, PAGE_SIZE);
        className = className==null?"":className;
        courseName = courseName==null?"":courseName;
        studentName = studentName==null?"":studentName;
//        IPage<StudentCourseItemVO> temp = mapper.getPage(page, className,courseName,studentName);
        return mapper.getPageForTea(page, className,courseName,studentName,teacherID);
    }

    public IPage<StudentCourseItemVO> getCourse(Integer index, String className, String courseName, String studentName) {
        Page<StudentCourseItemVO> page = new Page<>(index, PAGE_SIZE);
        className = className==null?"":className;
        courseName = courseName==null?"":courseName;
        studentName = studentName==null?"":studentName;
//        IPage<StudentCourseItemVO> temp = mapper.getPage(page, className,courseName,studentName);
        return mapper.getPage(page, className,courseName,studentName);
    }


    public int countByCourseId(Integer courseId) {
        LambdaQueryWrapper<Studentcourse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Studentcourse::getCoID, courseId);

        return mapper.selectCount(wrapper);
    }

    public int countByStudentId(Integer studentId) {
        LambdaQueryWrapper<Studentcourse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Studentcourse::getStuID, studentId);

        return mapper.selectCount(wrapper);
    }

    public Studentcourse getByCourseIdAndStudentId(Integer courseId, Integer studentId) {
        LambdaQueryWrapper<Studentcourse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Studentcourse::getCoID, courseId)
                .eq(Studentcourse::getStuID, studentId);

        return mapper.selectOne(wrapper);
    }

    public List<StudentCourseItemVO> listStudentCourseSelected(Integer studentId) {
        return mapper.listStudentCourseSelected(studentId);
    }

    public List<StudentExamItemVO> listStudentExam(Integer studentId) {
        return mapper.listStudentExam(studentId);
    }

    public Integer countStudentCourseSelectedByTimePart(Integer studentId, String timePart) {
        return mapper.countStudentCourseSelectedByTimePart(studentId, timePart);
    }

    public List<TimetableItemVO> listStudentTimetable(Integer studentId) {
        return mapper.listStudentTimetable(studentId);
    }

    public Integer countTeacherGrade(Integer teacherId, String courseName, String studentName) {
        return mapper.countTeacherGrade(teacherId, courseName, studentName);
    }

    public int deleteByCourseId(int id) {
        LambdaQueryWrapper<Studentcourse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Studentcourse::getCoID,id);
        return mapper.delete(wrapper);
    }

    /*public List<TeacherGradeItemVO> getTeacherGradePage(Integer index, Integer teacherId, String courseName, String studentName) {
        Page<TeacherGradeItemVO> page = new Page<>(index, PAGE_SIZE);
        return mapper.getTeacherGradePage(page, teacherId, courseName, studentName).getRecords();
    }*/

    public List<StudentSelectCourseItemVO> listStudentCourseSelected2(Integer studentId) {
        return mapper.listStudentSelectedCourse(studentId);
    }
}